<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2022-01-06 11:19:01
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-03-31 14:31:05
-->
<template>
  <div>
    <el-drawer
      title="项目信息"
      :visible.sync="dialog.projectDrawer"
      direction="rtl"
      :with-header="false"
      size="60%"
      :modal="false"
      @close="drawerClose"
    >
      <div class="head">
        <div class="header">
          <div class="img">
            <div style="font-size: 18px">详细信息</div>
          </div>
          <div class="btn">
            <el-button type="primary" size="default" @click="accomplish">
              完成
            </el-button>
            <el-button type="primary" size="default" @click="editTask">
              编辑
            </el-button>
            <el-button
              size="default"
              style="margin-right: 10px"
              @click="changeCharge"
            >
              更换负责人
            </el-button>
            <el-dropdown>
              <el-button class="el-icon-more"></el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="copyProject">
                  复制
                </el-dropdown-item>
                <el-dropdown-item @click.native="del"> 删除 </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="footer">
          <div class="information">
            <span>负责人</span>
            <h4>{{ drawerData.userName }}</h4>
          </div>
          <div class="information">
            <span>项目成员</span>
            <h4>{{ member }}</h4>
          </div>
          <div class="information">
            <span>开始时间</span>
            <h4>{{ drawerData.startTime }}</h4>
          </div>
          <div class="information">
            <span>结束时间</span>
            <h4>{{ drawerData.finishTime }}</h4>
          </div>
        </div>
      </div>
      <div class="content">
        <div class="left">
          <div class="left-head">
            <div class="left-head-task">任务清单</div>
            <div>
              <span style="font-size: 28px; cursor: pointer" @click="newList"
                >+</span
              >
            </div>
          </div>

          <div v-for="(item, index) in taskList" :key="index">
            <div
              class="taskList"
              :class="active == index ? 'active' : ''"
              @click="taskToggle(item, index)"
            >
              <div class="taskList-left">{{ index + 1 }}：{{ item.name }}</div>
              <div class="taskList-right">{{ item.finish }}/{{ item.all }}</div>
              <div class="taskList-hover">
                <el-dropdown>
                  <el-button class="el-icon-more"></el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="editList(item)">
                      编辑
                    </el-dropdown-item>
                    <el-dropdown-item
                      @click.native="delList(item)"
                      v-if="item.initial !== 1"
                    >
                      删除
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </div>
          </div>
        </div>
        <div class="right">
          <div
            class="right-head"
            v-if="taskList.length !== 0"
            @click.stop="newSubtask"
          >
            <div class="right-btn" v-if="!dialogObj.subtaskShow">
              + 请点击当前创建子任务内容
            </div>
            <div class="right-subtask" v-else>
              <div class="right-subtask-top">
                <el-form
                  :model="subtaskForm"
                  ref="subtaskForm"
                  :rules="subtaskRules"
                >
                  <el-form-item
                    class="right-subtask-top-ipt"
                    style="width: 100%"
                    prop="name"
                  >
                    <el-input
                      v-model="subtaskForm.name"
                      placeholder="请输入任务名称"
                    ></el-input>
                  </el-form-item>
                  <div class="right-subtask-bottom">
                    <div class="right-subtask-bottom-left">
                      <el-form-item prop="userId">
                        <el-select
                          v-model="subtaskForm.userId"
                          placeholder="请选择负责人"
                        >
                          <el-option
                            v-for="item in chargerOptions"
                            :key="item.userId"
                            :label="item.nickName"
                            :value="item.userId"
                          >
                          </el-option>
                        </el-select>
                      </el-form-item>
                      <el-form-item style="margin-left: 10px">
                        <el-date-picker
                          v-model="subtaskForm.finishTime"
                          type="datetime"
                          placeholder="选择日期"
                          value-format="yyyy-MM-dd HH:mm:ss"
                        >
                        </el-date-picker>
                      </el-form-item>
                    </div>
                    <div>
                      <el-button
                        type="primary"
                        style="height: 0.2rem"
                        size="default"
                        @click="onSubmit"
                        >创建</el-button
                      >
                    </div>
                  </div>
                </el-form>
              </div>
            </div>
            <!-- <el-button type="primary" size="default" @click="newSubtask">
              新建子任务
            </el-button> -->
          </div>

          <el-tree
            :data="Subtask"
            accordion
            :props="defaultProps"
            @node-click="handleNodeClick"
            highlight-current
          >
            <template slot-scope="{ node, data }">
              <div class="subTaskList">
                <div class="subTaskList-top">
                  <h3 @click="drawer(data, node)">
                    {{ data.name }}
                  </h3>
                  <div class="subTaskList-top-add">
                    <span
                      style="font-size: 28px; cursor: pointer"
                      @click="newSubListtask(data)"
                      >+</span
                    >
                  </div>
                </div>
                <div class="subTaskList-bottom">
                  <div class="endTime">
                    <span
                      :class="
                        bgColor(data) == '进行中'
                          ? 'endTime-status'
                          : bgColor(data) == '已完成'
                          ? 'endTime-status1'
                          : bgColor(data) == '已逾期'
                          ? 'endTime-status2'
                          : ''
                      "
                    >
                      {{ data | status }}
                    </span>
                    {{ data.finishTime }}
                  </div>
                  <div class="headImg">
                    <el-tooltip
                      :content="data.userName"
                      placement="top"
                      effect="dark"
                    >
                      <img :src="data.avatar" alt="" />
                    </el-tooltip>
                  </div>
                </div>
              </div>
            </template>
          </el-tree>
          <!-- <recursion
            :Subtask="Subtask"
            :dialogObj="dialogObj"
            :subtaskForm="subtaskForm"
            :getChargeList="getChargeList"
          ></recursion> -->
        </div>
      </div>
    </el-drawer>
    <el-dialog
      title="新增清单"
      :visible.sync="newListShow"
      width="30%"
      @close="listClose"
    >
      <el-form
        :model="detailedForm"
        ref="detailedForm"
        :rules="detailedRules"
        label-position="top"
      >
        <el-form-item label="清单名称" prop="name">
          <el-input v-model="detailedForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="newListShow = false">取消</el-button>
        <el-button type="primary" @click="listSubmit">创建</el-button>
      </span>
    </el-dialog>
    <!-- <el-dialog
      title="创建子任务"
      :visible.sync="dialogObj.subtaskShow"
      width="30%"
      @close="subtaskClose"
      v-if="dialogObj.subtaskShow"
    >
      <el-form
        :model="subtaskForm"
        ref="subtaskForm"
        :rules="subtaskRules"
        label-width="80px"
      >
        <el-form-item label="任务名称" style="width: 330px" prop="name">
          <el-input
            v-model="subtaskForm.name"
            placeholder="请输入任务名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="userId">
          <el-select v-model="subtaskForm.userId" placeholder="请选择负责人">
            <el-option
              v-for="item in chargerOptions"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="截止日期">
          <el-date-picker
            v-model="subtaskForm.finishTime"
            type="datetime"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="dialogObj.subtaskShow = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">创建</el-button>
      </span>
    </el-dialog> -->
    <drawer
      :dialog="dialog"
      :drawerData="drawerDataDaily"
      v-if="dialog.taskDrawerEject"
    ></drawer>
    <copy
      :dialog="dialog"
      :drawerData="drawerData"
      v-if="dialog.dialogCopyShowEject"
    ></copy>
  </div>
</template>

<script>
import {
  finish,
  orderList,
  orderEdit,
  orderDelete,
  subTaskList,
  subTaskAdd,
  copyProject,
} from "@/api/task/projectManagement";
import { distriBution } from "@/api/registration";
import recursion from "./recursion.vue";
import drawer from "../../dailyTask/components/drawer.vue";
import Copy from "./copy.vue";
export default {
  components: { recursion, drawer, Copy },
  props: {
    dialog: {
      type: Object,
      required: true,
    },
    drawerData: {
      type: Object,
      required: true,
    },
  },
  filters: {
    status: function (val) {
      let time = new Date().getTime();
      let time1 = new Date(val.finishTime).getTime();
      if (
        val.status === 3 ||
        (val.status === 1 && time1 < time && val.finishTime !== null)
      ) {
        val = "已逾期";
      } else if (val.status == 1) {
        val = "进行中";
      } else if (val.status == 2) {
        val = "已完成";
      }
      return val;
    },
  },
  watch: {
    drawerData: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val !== {}) {
          if (val.members !== null) {
            val.members.forEach((item) => {
              this.member += item.userName + ",";
            });
          }
        }
        this.member = this.member.slice(0, this.member.length - 1);
      },
    },
    "dialogObj.subtaskShow": {
      deep: true,
      handler(val) {
        if (val == false) {
          this.subtaskForm.name = null;
          this.subtaskForm.userId = null;
          this.subtaskForm.finishTime = null;
        }
      },
    },
  },
  data() {
    return {
      color: {
        bgColor: "rgba:(49,180,71,0.15)",
        color: "#31B447",
      },
      drawerDataDaily: {},
      defaultProps: {
        children: "children",
        label: "name",
      },
      member: "",
      chargerOptions: [],
      // 控制任务清单创建
      newListShow: false,
      dialogObj: {
        subtaskShow: false,
      },
      // 控制子任务创建

      active: 0,
      // 子任务表单
      subtaskForm: {
        parentId: 0,
        taskProjectId: this.drawerData.id,
        taskProjectOrderId: null,
        name: null,
        userId: null,
        finishTime: null,
      },
      //   清单名称form
      detailedForm: {
        name: "",
        taskProjectId: this.drawerData.id,
        id: null,
      },
      detailedRules: {
        name: [
          {
            required: true,
            message: "请输入清单名称",
            trigger: "blur",
          },
        ],
      },
      // 新建子任务规则
      subtaskRules: {
        userId: [
          {
            required: true,
            message: "请选择负责人",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入任务名称",
            trigger: "blur",
          },
        ],
      },
      // 子任务清单
      Subtask: [],
      //   任务清单
      taskList: [],
    };
  },
  created() {
    this.getTaskList();
  },
  mounted() {
    document.addEventListener("click", this.bodyCloseMenus);
  },
  beforeDestroy() {
    document.removeEventListener("click", this.bodyCloseMenus);
  },
  methods: {
    bgColor(item) {
      let val = "";
      let time = new Date().getTime();
      let time1 = new Date(item.finishTime).getTime();
      if (
        item.status === 3 ||
        (item.status === 1 && time1 < time && item.finishTime !== null)
      ) {
        val = "已逾期";
      } else if (item.status == 1) {
        val = "进行中";
      } else if (item.status == 2) {
        val = "已完成";
      }
      console.log(val);
      return val;
    },
    // 复制当前项目
    async copyProject() {
      this.dialog.dialogCopyShowEject = true;
      this.$nextTick(function () {
        this.dialog.dialogCopyShow = true;
      });
      // console.log(this.drawerData.id);
      // const confirmResult = await this.$confirm(
      //   "是否确认复制该项目, 是否继续?",
      //   "提示",
      //   {
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     type: "warning",
      //   }
      // ).catch((err) => err);
      // /*如果用户确认打印confirm,如果用户取消显示cancel*/
      // if (confirmResult !== "confirm") {
      //   return this.$message.info("已取消复制!");
      // } else {
      //   copyProject({ id: this.drawerData.id }).then((res) => {
      //     this.$message.success(res.msg);
      //     this.$parent.getList();
      //     this.dialog.projectDrawer = false;
      //   });
      // }
    },
    // 点击空白
    bodyCloseMenus(e) {
      let self = this;
      if (self.dialogObj.subtaskShow == true) {
        self.dialogObj.subtaskShow = false;
      }
    },
    drawer(d, item) {
      console.log(d, item);
      this.drawerDataDaily = d;
      console.log(this.drawerDataDaily);
      this.dialog.taskDrawerEject = true;
      this.$nextTick(function () {
        this.dialog.taskDrawer = true;
      });
    },
    // 新增子任务下任务
    newSubListtask(item) {
      console.log(1);
      this.dialogObj.subtaskShow = true;
      this.subtaskForm.parentId = item.id;
      this.getChargeList();
    },
    handleNodeClick(data) {
      // console.log(data);
    },
    // 删除
    del() {
      this.$parent.chargeId = this.drawerData.id;
      this.$parent.del();
    },
    //获取负责人
    getChargeList() {
      distriBution().then((res) => {
        this.chargerOptions = res.data;
      });
    },
    // 关闭子任务
    subtaskClose() {
      this.subtaskForm.name = null;
      this.subtaskForm.userId = null;
      this.subtaskForm.finishTime = null;
    },
    // 确认创建子任务
    onSubmit() {
      this.$refs.subtaskForm.validate((valid) => {
        if (!valid) return;
        subTaskAdd({
          parentId: this.subtaskForm.parentId,
          taskProjectId: this.subtaskForm.taskProjectId,
          taskProjectOrderId: this.subtaskForm.taskProjectOrderId,
          name: this.subtaskForm.name,
          userId: this.subtaskForm.userId,
          finishTime: this.subtaskForm.finishTime,
          loop: 2,
        }).then((res) => {
          this.$message.success(res.msg);
          this.dialogObj.subtaskShow = false;
          this.getsubTaskList();
          this.getTaskList();
          this.subtaskForm.name = "";
          this.subtaskForm.userId = "";
          this.subtaskForm.finishTime = "";
        });
      });
    },
    // 新建子任务
    newSubtask() {
      this.dialogObj.subtaskShow = true;
      this.getChargeList();
    },
    // 获取子任务列表
    getsubTaskList(id) {
      subTaskList({
        taskProjectId: this.drawerData.id,
        taskProjectOrderId: id,
        loop: 2,
      }).then((res) => {
        this.Subtask = res.data;
      });
    },
    // 新建清单关闭
    listClose() {
      this.detailedForm = {
        name: "",
        taskProjectId: this.drawerData.id,
        id: null,
      };
      this.getTaskList();
    },
    // 删除任务清单
    async delList(item) {
      const confirmResult = await this.$confirm(
        "删除该项目清单, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除!");
      } else {
        orderDelete({
          id: item.id,
          taskProjectId: this.drawerData.id,
        }).then((res) => {
          this.$message.success(res.msg);
          this.getTaskList();
        });
      }
    },
    // 确定创建新清单
    listSubmit() {
      orderEdit(this.detailedForm).then((res) => {
        this.$message.success(res.msg);
        this.newListShow = false;
        this.getTaskList();
      });
    },
    // 获取任务清单
    getTaskList() {
      orderList({ taskProjectId: this.drawerData.id }).then((res) => {
        this.taskList = res.data;
        if (res.data.length > 0) {
          this.taskToggle(res.data[0], 0);
        }
      });
    },
    //   新建清单
    newList() {
      this.newListShow = true;
    },
    //   任务清单切换
    taskToggle(item, i) {
      this.active = i;
      this.getsubTaskList(item.id);
      this.subtaskForm.taskProjectOrderId = item.id;
    },
    // 编辑单独清单
    editList(item) {
      this.newListShow = true;
      this.detailedForm = item;
    },
    // 编辑任务
    editTask() {
      this.$parent.drawerData = this.drawerData;
      this.$parent.editProject();
    },
    // 完成
    async accomplish() {
      const confirmResult = await this.$confirm(
        "完成该项目, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消!");
      } else {
        finish({
          id: this.drawerData.id,
        }).then((res) => {
          this.$message.success(res.msg);
          this.$parent.getList();
        });
      }
    },
    // 更换负责人
    changeCharge() {
      this.$parent.charge = this.drawerData.userId;
      this.$parent.changeCharge();
    },
    // 关闭抽屉
    drawerClose() {
      this.dialog.projectDrawerEject = false;
    },
  },
};
</script>

<style lang="less" scoped>
// /deep/.el-drawer.rtl {
//   height: 96%;
//   margin-top: 4%;
// }
.head {
  padding: 29px;
  width: 100%;
  height: 155px;
  border-bottom: 1px solid #ececec;
  span {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #999999;
  }
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .img {
      font-weight: 500;
      transform: translateY(-15px);
      img {
        vertical-align: middle;
        margin-right: 12px;
      }
      h3 {
        display: inline-block;
      }
    }
  }
  .footer {
    display: flex;
    .information {
      display: flex;
      flex-direction: column;
      margin-right: 80px;
      font-size: 14px;
      span {
        font-size: 14px;
      }
      h4 {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #2b2b2b;
        margin-top: 12px;
      }
    }
  }
}
.content {
  width: 100%;
  height: 77vh;
  display: flex;
  justify-content: space-around;
  margin-top: 20px;
  .left {
    padding: 20px;
    border: 1px solid #ececec;
    width: 25%;
    height: 720px;
    overflow: auto;
    .left-head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 16px;
      font-weight: 500;
    }
    .taskList {
      margin-top: 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      height: 50px;
    }
    .taskList-hover {
      display: none;
    }
    .taskList:hover {
      background-color: #f0f4ff;
      .taskList-right {
        display: none;
      }
      .taskList-hover {
        display: block;
      }
    }
  }
  .right {
    padding: 20px;
    border: 1px solid #ececec;
    width: 70%;
    height: 720px;
    overflow: auto;
    .right-btn {
      width: 100%;
      height: 60px;
      border: 1px solid #ececec;
      line-height: 60px;
      font-size: 18px;
      padding-left: 20px;
      color: #d2d2d2;
      cursor: pointer;
    }
    .right-subtask {
      width: 100%;
      height: 110px;
      padding-left: 10px;
      padding-right: 10px;
      border: 1px solid #ececec;
      .right-subtask-top-ipt {
        /deep/ .el-input__inner {
          border: none;
        }
      }
      .right-subtask-bottom {
        display: flex;
        justify-content: space-between;
        .right-subtask-bottom-left {
          display: flex;
        }
      }
    }
  }
}
.active {
  background-color: #f0f4ff;
}
.subTaskList {
  margin-top: 20px;
  width: 100%;
  height: 80px;
  border-bottom: 1px solid #ececec;
  .subTaskList-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 30px;
    h3 {
      font-size: 16px;
      padding-left: 20px;
      cursor: pointer;
      color: #1267fa;
    }
    .subTaskList-top-add {
      margin-right: 30px;
      display: none;
    }
  }
  .subTaskList-top:hover {
    .subTaskList-top-add {
      display: block;
    }
  }
  .subTaskList-bottom {
    margin-top: 10px;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
    .endTime {
      line-height: 35px;
      font-size: 14px;
      .endTime-status {
        background-color: rgba(18, 103, 250, 0.15);
        color: #1267fa;
        display: inline-block;
        height: 24px;
        line-height: 24px;
        padding: 0 10px;
        margin-right: 20px;
        border-radius: 11px;
      }
      .endTime-status1 {
        background-color: rgba(49, 180, 71, 0.15);
        color: #31b447;
        display: inline-block;
        height: 24px;
        line-height: 24px;
        padding: 0 10px;
        margin-right: 20px;
        border-radius: 11px;
      }
      .endTime-status2 {
        background-color: rgba(250, 30, 18, 0.15);
        color: #fa1e12;
        display: inline-block;
        height: 24px;
        line-height: 24px;
        padding: 0 10px;
        margin-right: 20px;
        border-radius: 11px;
      }
    }
    .headImg {
      width: 35px;
      height: 35px;
      border-radius: 35px;
      // background-color: #ff3040;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
.subTaskList:last-child {
  margin-bottom: 20px;
}
.el-tree {
  margin-top: 10px;
}
/deep/ .el-tree-node__content {
  height: 80px;
}
</style>